
#include "log_dialog.h"
#include "mainwindow.h"
#include "mydialog.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QIcon>
#include <QAction>
#include <QMessageBox>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QFileDialog>
#include <QInputDialog>
#include <QProgressDialog>
#include <QTimer>
#include <QString>
#include "qjson.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setMaximumSize(1800,1600); //设置窗口最大尺寸
    setMinimumSize(300,300); //设置窗口最小尺寸为
//    setFixedSize(800,600);

    setWindowTitle("设置窗口标题");

    QIcon icon(":/pic/pic1");
    setWindowIcon(icon);   //资源文件中设置图标

    connect(this,&MainWindow::windowTitleChanged,this,[=](){
        qDebug() <<"标题改变了！！！！！！！！！！";
    });

    setContextMenuPolicy(Qt::CustomContextMenu);
    //右键菜单
    connect(this , &MainWindow::customContextMenuRequested,this ,[](const QPoint &pos){
        QMenu menu;
        menu.addAction("选项1");
        menu.addAction("选项2");
        QPoint cursorPos = QCursor::pos();
        menu.exec(cursorPos);
        qDebug()<<"当前光标的位置"<<cursorPos.x()<<cursorPos.y();
    });
    //状态栏添加点击显示
    connect(ui->action_save, &QAction::triggered ,this ,[=](){
        QMessageBox::information(this ,"clicked","成功点击");
    });
    connect(ui->action_new, &QAction::triggered ,this ,[=](){
        QMessageBox::information(this ,"clicked","成功点击");
    });
    //工具栏中添加了按钮和文本框
    ui->toolBar->addWidget(new QPushButton("搜索"));
    QLineEdit* edit = new QLineEdit;
    edit->setMaximumWidth(200);
    edit->setFixedWidth(100);
    ui->toolBar->addWidget(edit);

    //添加第二个工具栏
    QToolBar* toolbar = new QToolBar("toolbar");
    this->addToolBar(Qt::LeftToolBarArea,toolbar);

    //状态栏添加子控件
    ui->statusbar->showMessage("这是状态栏");
    QPushButton* button = new QPushButton("按钮");
    ui->statusbar->addWidget(button);

    QLabel* label = new QLabel("label");
    ui->statusbar->addWidget(label);

}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_get_position_clicked()
{
    QRect rect_mainwindow = this->frameGeometry();
    qDebug()<<"左上角"<<rect_mainwindow.topLeft()
             <<"右上角"<<rect_mainwindow.topRight()
             <<"左下角"<<rect_mainwindow.bottomLeft()
             <<"右下角"<<rect_mainwindow.bottomRight()
             <<"宽度"<<rect_mainwindow.width()
             <<"高度"<<rect_mainwindow.height()  ;
}

void MainWindow::on_revise_size_and_position_clicked()
{
    int x = 100 +rand() % 500 ;
    int y = 100 +rand() % 500 ;
    int width = this->width() + 10;
    int heigth = this->height() + 10;
    setGeometry(x,y,width,heigth);
}


void MainWindow::on_revise_title_clicked()
{
    setWindowTitle("你好");
//    setWindowIcon();   //修改图标
}


void MainWindow::on_show_modal_dialog_clicked()
{
    MyDialog my_dlg;
    connect(&my_dlg,&MyDialog::finished,this ,[](int res){       //这边显示的返回值是ecec函数的返回值  点击不同的返回，有不同的返回值
            qDebug()<<"res="<<res;
        });

    int ret = my_dlg.exec();
    if (ret == QDialog::Accepted){
        qDebug()<<"accept";
    }
    else if(ret == QDialog::Rejected){
        qDebug()<<"rejected";
    }
    else
    {
        qDebug()<<"done";
    }

}


void MainWindow::on_QFileDialog_clicked()
{
#define SAVEFILE
#ifdef OPENDIR
    QString dirName = QFileDialog::getExistingDirectory(this , "打开目录0","D:\\bbbbbbcccccccccc\\QT_code\\test_sum_");
    QMessageBox::information(this,"打开目录1","您选择的目录是：" + dirName);
#endif \
    //注意双斜杠
#ifdef OPENFILE
    QString arg("Text files (*.txt)");
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),"D:\\bbbbbbcccccccccc\\QT_code\\test_sum_",
                                                    tr("Images (*.png *,jpg);;Text files(*.txt)"),&arg);
    QMessageBox::information(this , "打开文件", "您选择的文件是:" + fileName);
#endif

#ifdef OPENFILES
    QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open File"),"D:\\bbbbbbcccccccccc\\QT_code\\test_sum_"
                                                          ,tr("Images (*.png *,jpg);;Text files(*.txt)"));
    QString names;
    for(int i=0; i<fileNames.size();++i)
    {
        names += fileNames.at(i) + "";
    }
    QMessageBox::information(this , "打开文件(s)","您选择的文件是：" + names);
#endif

#ifdef SAVEFILE
    QString fileName = QFileDialog::getSaveFileName(this, "保存文件","D:\\bbbbbbcccccccccc\\QT_code\\test_sum_");
    QMessageBox::information(this , "保存文件","您指定的保存数据的文件时："+ fileName);

#endif
}


void MainWindow::on_InputDialog_clicked()
{
#define MULTITEXT
#ifdef INT
    int ret = QInputDialog::getInt(this , "年龄" , "您当前的年龄是" , 10,1,100,2);  //后四位数为 默认数，最小值，最大值，步长
    QMessageBox::information(this , "年龄" , "您当前的年龄：" + QString::number(ret));

#endif

#ifdef DOUBLE
    double ret = QInputDialog::getDouble(this , "薪资", "您的工资：",2000,6000,1000,2); //后四位数为 默认数，最小值，最大值，浮点数精度（保留到小数点后几位）
    QMessageBox::information(this , "薪资"  , "您当前的薪资：" + QString::number(ret));
#endif

#ifdef ITEM
   QStringList items;
   items <<"apple"<<"banana"<<"orange";
   QString item = QInputDialog::getItem(this , "请选择你喜欢的水果", "你喜欢的水果", items ,1,false); //  选择的item，默认是第几个，是否能修改
   QMessageBox::information(this , "水果" , "您最喜欢的水果是：" + item);
#endif

#ifdef TEXT
   QString text = QInputDialog::getText(this , "密码" , "请输入新的密码：" ,QLineEdit::Password, "hello world" );
   QMessageBox::information(this , "密码", "您设置的密码是：" + text);
#endif

#ifdef MULTITEXT
   QString info = QInputDialog::getMultiLineText(this , "表白", "你说啥？");
   QMessageBox::information(this , "雪落无尘处" , "过去了过去了：" + info);
#endif
}


void MainWindow::on_QProgressDialog_clicked()
{
   //1. 创建进度条对话框窗口
   QProgressDialog *progress = new QProgressDialog("正在拷贝数据...","取消拷贝",0,100,this);
   //2.初始化并显示进度条窗口
   progress->setWindowTitle("请稍后");
   progress->setWindowModality(Qt::WindowModal);  //将弹出的进度条窗口设置为模态窗口
   progress->show();
   //3.更新进度条
   static int value = 0;
   QTimer *timer = new QTimer(this);
   connect(timer , &QTimer::timeout , this ,[=](){
       progress->setValue(value);
       value++;
       if(value > progress->maximum())
       {
           timer->stop();
           value = 0;
       }
   });

   connect(progress, &QProgressDialog::canceled,this,[=](){
       timer->stop();
       value = 0;
   });
   timer->start(50);
}

//-------------------------------------------------消息对话框的使用
void MainWindow::on_message_box_clicked()
{
    QMessageBox::about(this , "about--------","这是一个简单的消息提示框");
    QMessageBox::critical(this , "critical", "这是一个错误提示框");

    int ret = QMessageBox::question(this , "question" ,"你要保存修改的文件的内容吗？？？",QMessageBox::Save|QMessageBox::Cancel,QMessageBox::Cancel);
    if(ret==QMessageBox::Save)
    {
        QMessageBox::information(this , "information","恭喜您保存成功");
    }
    else if(ret ==QMessageBox::Cancel){
        QMessageBox::warning(this,"warning","你放弃了保存");
    }
}



void MainWindow::on_show_log_clicked()
{
    log_dialog log_dlg;
    log_dlg.exec();
}


void MainWindow::on_string_button_clicked()
{
    QString str = QString("%1   %2    %1").arg("替换1").arg("替换2");
    qDebug() << str;

    QString tmp1=QString("芜湖起飞，哈哈哈...");
    QByteArray tmp2 = QByteArray("芜湖起飞,哈哈哈...");
    qDebug()<< tmp1.length();
    qDebug()<< tmp2.length();
}


void MainWindow::on_qjson_button_clicked()
{
    QJsonDocument doc;
    if(doc.isEmpty())
    {
        qDebug()<<"json document is empty";
    }
    if (doc.isNull())
    {
        qDebug()<<"json document is null";
    }
    qjson::WriteJson();
    qjson::ReadJson();
}

